package com.erp.erp_ui.CarryPlan;

import com.erp.erp_entitys.CarryPlan.CarryPlan;
import com.erp.erp_entitys.Company.CompanyEntity;
import com.erp.erp_entitys.Customer.CustomerEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.CarryPlan.CarryPlanService;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出运计划主信息表(CarryPlan)表控制层
 *
 * @author makejava
 * @since 2020-10-15 23:31:21
 */
@Controller
@RequestMapping(value = "carryPlan")
public class CarryPlanController {
    /**
     * 服务对象
     */
    @Resource
    private CarryPlanService carryPlanService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "selectOne")
    @ResponseBody
    public Object selectOne(Integer id) {
        try{
            return R.build(this.carryPlanService.queryById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    @RequestMapping(value = "select")
    @ResponseBody
    public Object selectCarryPlan(Integer documenttype,String documentsinformation,String subordinatesalesman
            ,String owneroperator,String startsdate,String endsdate,String signingcompany,String startshdate,String endshdate,int page,int limit){
        try{

            documentsinformation=this.conver(documentsinformation);
            subordinatesalesman=this.conver(subordinatesalesman);
            owneroperator=this.conver(owneroperator);
            startsdate=this.conver(startsdate);
            endsdate=this.conver(endsdate);
            signingcompany=this.conver(signingcompany);
            startshdate=this.conver(startshdate);
            endshdate=this.conver(endshdate);
            PageInfo<CarryPlan> pageInfoList = carryPlanService.pageSelect(documenttype,documentsinformation,
                    subordinatesalesman,owneroperator,startsdate,
                    endsdate,signingcompany,startshdate,
                    endshdate,page,limit);
            return R.build(200, "success", pageInfoList.getTotal(), pageInfoList.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }


    /**
     * 出运计划新增
     * @param carryPlan
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public Object saveCarryPlan(@RequestBody CarryPlan carryPlan){
        try{
            if(carryPlan.getId()!=null){
                return R.build(this.carryPlanService.update(carryPlan));
            }else{
                return R.build(this.carryPlanService.insert(carryPlan));
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 出运计划修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "updates",method = RequestMethod.POST)
    @ResponseBody
    public Object updateCarryPlans(@RequestParam(value="id",required= true) Integer id,@RequestParam(value="statuss",required= true) String status){

        try{
            CarryPlan carryPlan = this.carryPlanService.queryById(id);
            if(carryPlan!=null){
                carryPlan.setStatus(status);
            }
            return R.build(this.carryPlanService.update(carryPlan));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 出运计划删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public Object deleteCarryPlan(@RequestParam(value="id",required= true) Integer id){
        try{
            return R.build(this.carryPlanService.deleteById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }


    /**
     * 获取公司
     * @return
     */
    @RequestMapping(value = "getCompany")
    @ResponseBody
    public Object selectCompany() {
        List<CompanyEntity> list = this.carryPlanService.getCompany();
        return R.build(list);
    }

    /**
     * 获取客户
     * @return
     */
    @RequestMapping(value = "getCustomer")
    @ResponseBody
    public Object selectCustomer() {
        List<CustomerEntity> list = this.carryPlanService.getCustomer();
        return R.build(list);
    }

    /**
     * 获取用户
     * @return
     */
    @RequestMapping(value = "getUser")
    @ResponseBody
    public Object selectUser() {
        List<UserEntity> list = this.carryPlanService.getUser();
        return R.build(list);
    }

    private String conver(String obj){
        if(obj !=null && obj.equals("")){
            return null;
        }else{
            return obj;
        }

    }

}